﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.UPMS.Model.BusinessPart;
using ZDSoft.UPMS.Service.BusinessPart;
using ZDSoft.UPMS.Model.SystemPart;
using ZDSoft.UPMS.Service.SystemPart;
using ZDSoft.UPMS.Core;
using NHibernate.Criterion;
using ZDSoft.UPMS.Model;
using ZDSoft.UPMS.Web.Helper;


namespace ZDSoft.UPMS.Web.Controllers
{
        public class SituationController : Controller
        {
            /// <summary>
            /// 实例化情况类
            /// </summary>
            Situation situation = new Situation();
            /// <summary>
            ///增加学生情况视图
            /// </summary>
            /// <returns>返回学生情况数据</returns>
            public ActionResult Create()
            {
                IList<Organization> teachingClasses = null;
                ViewBag.Course = FindCourse(ref teachingClasses);
                ViewBag.Organization = teachingClasses;
                TempData["Student"] = FindStudent(teachingClasses);
                TempData["ParentsType"] = SituationTypeParents();
                return View();
            }
            [HttpPost]
            public ActionResult Create(string selectMsg, string ddlCourse, string ddlOrganization, string inputTime, string txtRemark, string txtUserNumber, string txtTypeID)
            {
                SituationCreate(selectMsg, ddlCourse, inputTime, ddlOrganization, txtRemark, txtUserNumber, txtTypeID);
                return View();
            }

            /// <summary>
            /// 修改学生情况信息
            /// </summary>
            /// <returns></returns>
            public ActionResult Update()
            {
                ViewBag.SituationType = SituationTypeParents();
                IList<Organization> teachingClasses = null;
                ViewBag.Course = FindCourse(ref teachingClasses);
                ViewBag.Courses = Container.Instance.Resolve<ICourseService>().FindAll();
                ViewBag.Organization = teachingClasses;
                TempData["Student"] = FindStudent(teachingClasses);
                TempData["ParentsType"] = SituationTypeParents();
                ViewBag.Term = Container.Instance.Resolve<ITermService>().FindAll();
                return View();
            }
            public string Updates(string siID, string countMsg, string remarks)
            {
                Container.Instance.Resolve<ISituationService>().Update(siID, countMsg, remarks);
                FindSituationByID(int.Parse(siID));
                return "修改成功！";
            }

            /// <summary>
            /// 教师所教课程
            /// </summary>
            /// <returns></returns>
            public IList<TeacherTeachingCourse> FindUserTeacherTeachingCourser()
            {
                IList<ICriterion> queryConditions = new List<ICriterion>();
                queryConditions.Add(Expression.Eq("Teacher.ID", Helper.AppHelper.LoginedUser.ID));
                IList<TeacherTeachingCourse> ttcList = Container.Instance.Resolve<ITeacherTeachingCourseService>().Find(queryConditions);
                IList<TeacherTeachingCourse> tCList = null;
                if (getlocalDate() != 0)
                {
                    for (int i = 0; i < ttcList.Count; i++)
                    {
                        if (ttcList[i].Term.ID == getlocalDate())
                        {
                            tCList.Add(ttcList[i]);
                        }
                    }
                }
                return tCList;
            }

            /// <summary>
            /// 查找所以情况类型
            /// </summary>
            /// <returns></returns>
            public IList<Situation> FindSituationAll()
            {
                IList<Situation> siList = Container.Instance.Resolve<ISituationService>().FindAll();
                return siList;
            }

            /// <summary>
            /// 查找登录教师所教学生
            /// </summary>
            /// <returns></returns>
            public IList<User> FindStudent(IList<Organization> teachingClasses)
            {
                IList<User> students = null;
                IList<QueryConditions> conditions = new List<QueryConditions>();
                if (teachingClasses.Count != 0)
                {
                    conditions.Add(new QueryConditions() { PropertyName = "Organization.ID", Operator = "=", Value = teachingClasses[0].ID });
                    students = Container.Instance.Resolve<IUserService>().Find(conditions);
                }
                return students;
            }

            /// <summary>
            /// 查找登录用户所教课程
            /// </summary>
            /// <returns>登录用户所教课程</returns>
            public IList<Course> FindCourse(ref IList<Organization> teachingClasses)
            {
                IList<Course> courseList = new List<Course>();
                teachingClasses = new List<Organization>();
                IList<QueryConditions> conditions = new List<QueryConditions>();
                conditions.Add(new QueryConditions() { PropertyName = "Teacher.ID", Operator = "=", Value = AppHelper.LoginedUser.ID });
                IList<TeacherTeachingCourse> ttcList = Container.Instance.Resolve<ITeacherTeachingCourseService>().Find(conditions);
                if (getlocalDate() != 0)
                {
                    foreach (TeacherTeachingCourse t in ttcList)
                    {
                        if (t.Term.ID == getlocalDate())
                        {
                            courseList.Add(t.Course);
                            teachingClasses.Add(t.Classes);
                        }
                    }
                }
                else
                {
                    courseList = null;
                }
                return courseList;

            }

            /// <summary>
            /// 情况类型叶子节点
            /// </summary>
            /// <returns>情况类型所有叶子节点</returns>
            public IList<SituationType> SituationTypeLeaves()
            {
                IList<SituationType> typeList = Container.Instance.Resolve<ISituationTypeService>().FindSituationTypeLeaves();
                return typeList;
            }

            /// <summary>
            /// 情况类型父类
            /// </summary>
            /// <returns></returns>
            public IList<SituationType> SituationTypeParents()
            {
                IList<SituationType> parentsList = Container.Instance.Resolve<ISituationTypeService>().FindSituationTypeParents();
                return parentsList;
            }

            /// <summary>
            /// 新建学生情况
            /// </summary>
            /// <param name="selectMsg">情况次数</param>
            /// <param name="ddlCourse">课程</param>
            /// <param name="inputTime">录入时间</param>
            /// <param name="ddlOrganization">组织机构</param>
            /// <param name="txtRemark">备注信息</param>
            /// <param name="txtUserNumber">学生ID</param>
            /// <param name="txtTypeID">情况类型</param>
            public void SituationCreate(string selectMsg, string ddlCourse, string inputTime, string ddlOrganization, string txtRemark, string txtUserNumber, string txtTypeID)
            {
                Container.Instance.Resolve<ISituationService>().Create(selectMsg, ddlCourse, inputTime, ddlOrganization, txtRemark, txtUserNumber, txtTypeID, AppHelper.LoginedUser.ID);

            }

            /// <summary>
            /// 默认情况
            /// </summary>
            /// <returns></returns>
            public int FindTypeFirstID()
            {
                int typeID = (SituationTypeParents().FirstOrDefault()).ID;
                return typeID;
            }

            /// <summary>
            /// 查找已添加日期
            /// </summary>
            /// <returns></returns>
            public IList<string> FindDate()
            {
                IList<string> dateList = new List<string>();
                IList<UserCourseSituation> UserCourseSituationTypeList = Container.Instance.Resolve<IUserCourseSituationService>().FindAll();
                foreach (UserCourseSituation ucs in UserCourseSituationTypeList)
                {
                    dateList.Add((ucs.Date).ToString());
                }
                return dateList;
            }

            public ActionResult FindByParentID(string ddlterms, string ddlSituationType, string ddlCourse, string stuNumber)
            {
                IList<Situation> situations = Container.Instance.Resolve<ISituationService>().FindEqualConditionsSituation(ddlterms, ddlSituationType, ddlCourse, stuNumber);
                IList<string> situationInfo = new List<string>();

                for (int i = 0; i < situations.Count; i++)
                {
                    situationInfo.Add(situations[i].ID.ToString());
                    situationInfo.Add(situations[i].Student.UserNumber);
                    situationInfo.Add(situations[i].Student.Name);
                    situationInfo.Add(situations[i].SituationType.Name);
                    situationInfo.Add(situations[i].SelectCount.ToString());
                    situationInfo.Add(situations[i].userCourseSituation.Date.ToShortDateString());
                    situationInfo.Add(situations[i].Remark);
                }
                return Json(situationInfo, JsonRequestBehavior.AllowGet);

            }

            public IList<string> FindData()
            {
                IList<string> ss = new List<string>();
                return ss;
            }
            public ActionResult FindSituationByID(int situationID)
            {
                IList<string> situation = Container.Instance.Resolve<ISituationService>().FindSituationByID(situationID);
                return Json(situation, JsonRequestBehavior.AllowGet);
            }

            public int getlocalDate()
            {
                DateTime dd = DateTime.Now;
                string id = null;
                IList<Term> tList = Container.Instance.Resolve<ITermService>().FindAll();
                for (int i = 0; i < tList.Count; i++)
                {
                    if (tList[i].StartTime <= dd && tList[i].StopTime >= dd)
                    {
                        id = tList[i].ID.ToString();
                    }
                }
                if (id == null)
                {
                    id = 0.ToString();
                }
                return int.Parse(id);
            }

        }


    }

